package jianzhi

func verifyPostorder(postorder []int) bool {

	if len(postorder) == 0 {
		return true
	}

	root := postorder[len(postorder)-1]

	var i int

	for i = root - 1; i >= 0; i-- {
		if postorder[i] < root {
			break
		}
	}

	var left, right bool

	if i == -1 {
		left = true
		right = verifyPostorder(postorder[:len(postorder)-1])
	} else {
		for j := 0; j <= i; j++ {
			if postorder[j] > root {
				return false
			}
		}

		right = verifyPostorder(postorder[i+1 : len(postorder)-1])
		left = verifyPostorder(postorder[:i+1])

	}

	return right && left

}
